package dvdstore.client.gui.search;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;

import common.dvd.DVD;

import dvdstore.client.connector.ServerConnector;
import dvdstore.client.exception.ServerConnectionException;
import dvdstore.client.gui.dvd.DVDFields;

public class DVDSearchButton extends JButton{
	
	private final DVDSearchFieldsPanel parentPanel;
	
	public DVDSearchButton(String text, final DVDSearchFieldsPanel parentPanel) {
		super(text);
		this.parentPanel = parentPanel;
		addActionListener(new ActionListener() {
			 
            public void actionPerformed(ActionEvent e) 
            {
                String idFieldText = parentPanel.getFieldsMap().get(DVDFields.ID).getText();
            	if(idFieldText != null && ! "".equals(idFieldText) ) {
            		Long dvdID = Long.valueOf(idFieldText);
            		ServerConnector serverConnector = ServerConnector.getInstance();
            		DVD dvd = serverConnector.getDVDByID(dvdID);
            		if(dvd != null) {
            			System.out.println(dvd.toString());
            		} else {
            			System.out.println("DVD with id " + dvdID + " not found!");
            		}
            	}
            }
        
		});      
	}
}
